package com.soilflow.web.controller.reception;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.soilflow.common.domain.SysArticle;
import com.soilflow.common.domain.SysCollect;
import com.soilflow.common.domain.dto.SysColleectDto;
import com.soilflow.info.service.ISysCollectService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.soilflow.common.annotation.Log;
import com.soilflow.common.core.controller.BaseController;
import com.soilflow.common.core.domain.AjaxResult;
import com.soilflow.common.enums.BusinessType;
import com.soilflow.common.utils.poi.ExcelUtil;
import com.soilflow.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author party
 * @date 2023-10-09
 */
@RestController
@RequestMapping("/system/collect")
public class SysCollectController extends BaseController {
    @Autowired
    private ISysCollectService sysCollectService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:collect:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCollect sysCollect)
    {
        startPage();
        List<SysCollect> list = sysCollectService.selectSysCollectList(sysCollect);
        return getDataTable(list);
    }
    /**
     * 对文章按收藏次数进行排序
     */
    @GetMapping("/sortCollect")
    public TableDataInfo sortCollect(String sort){
        startPage();
        List<SysArticle> list = sysCollectService.sortCollect(sort);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        sysCollectService.export(response);
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:collect:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysCollectService.selectSysCollectById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:collect:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysColleectDto dto)
    {
        System.out.println("调用了当前接口");
        return toAjax(sysCollectService.insertSysCollect(dto));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:collect:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCollect sysCollect)
    {
        return toAjax(sysCollectService.updateSysCollect(sysCollect));
    }

    /**
     * 删除【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:collect:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysCollectService.deleteSysCollectByIds(ids));
    }

    @DeleteMapping("/article/{id}")
    public AjaxResult removearticleId(@PathVariable String id)
    {
        return toAjax(sysCollectService.removearticleId(id));
    }

    /**
     * 查询当前用户是否收藏了额这篇文章
     */
    @GetMapping("/userCollect/{id}/{userId}")
    public AjaxResult userCollect(@PathVariable("id") String id,@PathVariable("userId") Long userId)
    {
        return AjaxResult.success(sysCollectService.userCollect(id,userId));
    }
}
